package com.kzw.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.kzw.entity.Goods;
import com.kzw.entity.History;
import com.kzw.entity.Record;
import com.kzw.entity.User;
import com.kzw.service.GoodsService;
import com.kzw.service.HistoryService;
import com.kzw.service.RecordService;

import my.bean.Msg;
import my.db.Page;
import my.mvc.Annotation.JSON;
import my.mvc.RequestContext;
import my.util.BeanUtil;

@SuppressWarnings("unchecked")
public class GoodsAction {

	private GoodsService service = new GoodsService();
	private RecordService rservice = new RecordService();
	private HistoryService hservice = new HistoryService();

	/**
	 * 商品列表
	 * */
	@JSON
	public Page<Goods> list(RequestContext rc) {
		// 获得分页信息, layui表格分页参数：page、limit
		Page<Goods> page = rc.form(Page.class);
		String title = rc.param("title");
		service.list(page, title);		
		return page;
	}
	/**
	 * 记录列表
	 * */
	@JSON
	public Page<Record> listRecord(RequestContext rc) {
		// 获得分页信息, layui表格分页参数：page、limit
		Page<Record> page = rc.form(Page.class);
		String uname = rc.param("uname");
		rservice.list(page, uname);
		return page;
	}
	/**
	 * 历史列表
	 * */
	@JSON
	public Page<History> listHistory(RequestContext rc) {
		// 获得分页信息, layui表格分页参数：page、limit
		Page<History> page = rc.form(Page.class);
		String uname = rc.param("uname");
		hservice.list(page, uname);		
		return page;
	}
	
	@JSON
	public void list2(RequestContext rc) {
		System.out.println("list2");
		System.out.println(service.findAll());
		rc.reqAttr("commodity",service.findAll());
		rc.forward("/frontPage/commodity.jsp");
	}
	
	
	/**
	 * 保存
	 * */
	@JSON
	public Msg save(RequestContext rc) throws Exception {
		User user = (User)rc.sessionAttr("USER");
		Goods a = rc.form(Goods.class);
		if(a.getId() == 0) {
			if(a.getCtime() == null) {
				a.setCtime(new Date());
			}
			a.setAuthor(user.getUname());
			a.setAuthorId(user.getId());
			service.save(a);
		} else {
			Goods orgA = service.get(a.getId());
			BeanUtil.copyNotNullProperties(orgA, a);
			service.update(orgA);
		}
		
		return new Msg(true);
	}
	
	/**
	 * 获得商品列表
	 * */
	public void get(RequestContext rc) {
		long id = rc.param("id", 0L);
		Goods goods = service.get(id);
		rc.reqAttr("goods", goods);
		rc.forward("/page/goods/goods_form.jsp");
	}
	/**
	 * 删除商品
	 * */
	@JSON
	public Msg multiDel(RequestContext rc) {
		
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			String[] arr = ids.split(",");
			for(String id : arr) {
				System.out.println(id);
				service.delete(Long.parseLong(id));
			}
		}
		return new Msg();
	}
	/**
	 * 删除记录
	 * */
	@JSON
	public Msg multiDelRecord(RequestContext rc) {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			String[] arr = ids.split(",");
			for(String id : arr) {
				rservice.delete(Long.parseLong(id));
			}
		}
		return new Msg();
	}
	/**
	 * 删除历史
	 * */
	@JSON
	public Msg multiDelHistory(RequestContext rc) {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			String[] arr = ids.split(",");
			for(String id : arr) {
				hservice.delete(Long.parseLong(id));
			}
		}
		return new Msg();
	}
}
